package com.e3.basdoc.employee.client;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.e3.basdoc.bank.client.BankRefDialog;
import com.e3.basdoc.dept.client.DeptRefDialog;
import com.e3.basdoc.employee.model.EmployeeEducations;
import com.e3.basdoc.employee.model.EmployeeModel;
import com.e3.basdoc.employee.model.EmployeeStatus;
import com.e3.basdoc.employeecls.client.EmployeeClsRefDialog;
import com.e3.core.lang.DataConstants;
import com.e3.template.tgf.GFHelper;
import com.e3.web.widget.client.MyFormFieldFactory;
import com.e3.web.widget.client.MyGridHelper;
import com.e3.web.widget.model.ListTypeModel;
import com.extjs.gxt.ui.client.widget.form.Field;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.GridCellRenderer;
import com.extjs.gxt.ui.client.widget.grid.RowNumberer;

public class EmployeeHelper extends GFHelper<EmployeeModel>{
	
	public static EmployeeHelper INSTANCE = new EmployeeHelper();
	
	private String[] notNullFields = {EmployeeModel.CODE,EmployeeModel.NAME};
	
	private Map<String,Integer> lengthMap = new HashMap<String, Integer>();
	
	private EmployeeHelper(){
		lengthMap.put(EmployeeModel.ID, 19);
		lengthMap.put(EmployeeModel.CODE, 20);
		lengthMap.put(EmployeeModel.NAME, 50);
		lengthMap.put(EmployeeModel.FORMER_NAME, 50);
		lengthMap.put(EmployeeModel.MOBILE, 20);
		lengthMap.put(EmployeeModel.EMAIL, 50);
		lengthMap.put(EmployeeModel.BANK_ACCOUNT, 50);
		lengthMap.put(EmployeeModel.HOME_ADDRESS, 100);
		lengthMap.put(EmployeeModel.HOME_PHONE, 20);
		lengthMap.put(EmployeeModel.IDCARD_NO, 20);
		lengthMap.put(EmployeeModel.POSTCODE, 10);
		lengthMap.put(EmployeeModel.RANK, 50);
		lengthMap.put(EmployeeModel.SKILL_LEVEL, 50);
		lengthMap.put(EmployeeModel.OFFICE_PHONE, 20);
		lengthMap.put(EmployeeModel.REMARK, 100);
	}

	public List<Field<?>> getFormFields(String[] fNames){
		List<Field<?>> fields = new ArrayList<Field<?>>(fNames.length);
		for (String name:fNames) {
			Field<?> field = createFormField(name);
			fields.add(field);
		}
		return fields;
	}
	
	public Field<?> createFormField(String name){
		Field<?> formField = null;
		String dispName = getFieldDisplayName(name);
		boolean isNotNull = isNotNullField(name);
		
		if(name.equals(EmployeeModel.ID)){
			formField = MyFormFieldFactory.createIdField(name);
		}	
		else if(name.equals(EmployeeModel.CODE)||name.equals(EmployeeModel.NAME)
			||name.equals(EmployeeModel.FORMER_NAME)||name.equals(EmployeeModel.MOBILE)
			||name.equals(EmployeeModel.EMAIL)||name.equals(EmployeeModel.BANK_ACCOUNT)
			||name.equals(EmployeeModel.HOME_ADDRESS)||name.equals(EmployeeModel.HOME_PHONE)
			||name.equals(EmployeeModel.IDCARD_NO)||name.equals(EmployeeModel.POSTCODE)
			||name.equals(EmployeeModel.RANK)||name.equals(EmployeeModel.SKILL_LEVEL)
			||name.equals(EmployeeModel.OFFICE_PHONE)){
			int length = lengthMap.get(name);
			formField = MyFormFieldFactory.createStringField(name,dispName,length,isNotNull);
		}
		else if(name.equals(EmployeeModel.CLASSES)){
			formField = MyFormFieldFactory.createRefField(name,dispName,new EmployeeClsRefDialog(),isNotNull);
		}
		else if(name.equals(EmployeeModel.DEPT)){
			formField = MyFormFieldFactory.createRefField(name,dispName,new DeptRefDialog(),isNotNull);
		}
		else if(name.equals(EmployeeModel.BANK)){
			formField = MyFormFieldFactory.createRefField(name,dispName,new BankRefDialog(),isNotNull);
		}
		else if(name.equals(EmployeeModel.BIRTHDAY)||name.equals(EmployeeModel.QUIT_DATE)
				||name.equals(EmployeeModel.ONBOARD_DATE)||name.equals(EmployeeModel.WORK_FROM_DATE)){
			formField = MyFormFieldFactory.createDateField(name, dispName,isNotNull);
		}

		else if(name.equals(EmployeeModel.GENDER)){
			formField = MyFormFieldFactory.createGenderRadioGroupField(name, dispName);
		}
		else if(name.equals(EmployeeModel.EDUCATION)){
			formField = MyFormFieldFactory.createListField(name, dispName,getEducationListTypeModel() , true,isNotNull);
		}
		else if(name.equals(EmployeeModel.STATUS)){
			formField = MyFormFieldFactory.createListField(name, dispName, getStatusListTypeModel(), true,isNotNull);
		}
		else if(name.equals(EmployeeModel.REMARK)){
			int length = lengthMap.get(name);
			formField = MyFormFieldFactory.createTextAreaField(name, dispName,length,isNotNull);
		}
		
		return formField;
	}
	
	private List<ListTypeModel<Integer>> getEducationListTypeModel(){
		List<ListTypeModel<Integer>> statusValues = new ArrayList<ListTypeModel<Integer>>();
		Integer code = EmployeeEducations.EDU_POSTDOCTOR;
		ListTypeModel<Integer> ltModel = new ListTypeModel<Integer>(code, getFieldDisplayName(EmployeeModel.EDU_POSTDOCTOR_NAME));
		statusValues.add(ltModel);
		code = EmployeeEducations.EDU_DOCTOR;
		ltModel = new ListTypeModel<Integer>(code,getFieldDisplayName(EmployeeModel.EDU_DOCTOR_NAME));
		statusValues.add(ltModel);
		code = EmployeeEducations.EDU_MASTER;
		ltModel = new ListTypeModel<Integer>(code,getFieldDisplayName(EmployeeModel.EDU_MASTER_NAME));
		statusValues.add(ltModel);
		code = EmployeeEducations.EDU_BACHELOR;
		ltModel = new ListTypeModel<Integer>(code,getFieldDisplayName(EmployeeModel.EDU_BACHELOR_NAME));
		statusValues.add(ltModel);
		code = EmployeeEducations.EDU_DIPLOMA;
		ltModel = new ListTypeModel<Integer>(code,getFieldDisplayName(EmployeeModel.EDU_DIPLOMA_NAME));
		statusValues.add(ltModel);
		code = EmployeeEducations.EDU_OTHERS;
		ltModel = new ListTypeModel<Integer>(code,getFieldDisplayName(EmployeeModel.EDU_OTHERS_NAME));
		statusValues.add(ltModel);
		return statusValues;
	}
	
	private List<ListTypeModel<Integer>> getStatusListTypeModel(){
		List<ListTypeModel<Integer>> statusValues = new ArrayList<ListTypeModel<Integer>>();
		Integer code = EmployeeStatus.STATUS_NORMAL;
		ListTypeModel<Integer> ltModel = new ListTypeModel<Integer>(code, getFieldDisplayName(EmployeeModel.STATUS_NORMAL_NAME));
		statusValues.add(ltModel);
		code = EmployeeStatus.STATUS_ONLEAVE;
		ltModel = new ListTypeModel<Integer>(code,getFieldDisplayName(EmployeeModel.STATUS_ONLEAVE_NAME));
		statusValues.add(ltModel);
		code = EmployeeStatus.STATUS_QUITE;
		ltModel = new ListTypeModel<Integer>(code,getFieldDisplayName(EmployeeModel.STATUS_QUIT_NAME));
		statusValues.add(ltModel);
		return statusValues;
	}

	
	public List<ColumnConfig> getGridColumnConfigs(){
		List<ColumnConfig> configs = new ArrayList<ColumnConfig>();
		RowNumberer numberer = new RowNumberer();   
		configs.add(numberer);
		
		ColumnConfig column = new ColumnConfig();
		String name = EmployeeModel.CODE;
	    column.setId(name);   
	    column.setHeader(getFieldDisplayName(name));   
	    column.setWidth(200);   
	    configs.add(column);  
	    
	    column = new ColumnConfig();
		name = EmployeeModel.NAME;
	    column.setId(name);   
	    column.setHeader(getFieldDisplayName(name));   
	    column.setWidth(200);   
	    configs.add(column);  
	    
	    column = new ColumnConfig();
		name = EmployeeModel.FORMER_NAME;
	    column.setId(name);   
	    column.setHeader(getFieldDisplayName(name));   
	    column.setWidth(200);   
	    configs.add(column);
	    
		Object[] codes = {DataConstants.MALE,DataConstants.FEMALE};
		String[] dispNames = {EmployeeMessages.INSTANCE.lab_male(),EmployeeMessages.INSTANCE.lab_female()};
	    GridCellRenderer<EmployeeModel> genderRenderer = MyGridHelper.createListCellRender(EmployeeModel.GENDER, codes, dispNames);
	    column = new ColumnConfig();
		name = EmployeeModel.GENDER;
	    column.setId(name);   
	    column.setHeader(getFieldDisplayName(name));   
	    column.setWidth(200);
	    column.setRenderer(genderRenderer);
	    configs.add(column);
	    
	    column = new ColumnConfig();
		name = EmployeeModel.BIRTHDAY;
	    column.setId(name);   
	    column.setHeader(getFieldDisplayName(name));   
	    column.setWidth(200);   
	    configs.add(column);
	    
	    column = new ColumnConfig();
		name = EmployeeModel.DEPT;
	    column.setId(name);   
	    column.setHeader(getFieldDisplayName(name));   
	    column.setWidth(200);   
	    configs.add(column);

	    column = new ColumnConfig();
		name = EmployeeModel.CLASSES;
	    column.setId(name);   
	    column.setHeader(getFieldDisplayName(name));   
	    column.setWidth(200);   
	    configs.add(column);
	    
	    
		Object[] statusCodes = {EmployeeStatus.STATUS_NORMAL,EmployeeStatus.STATUS_ONLEAVE,EmployeeStatus.STATUS_QUITE};
		String s1 = getFieldDisplayName(EmployeeModel.STATUS_NORMAL_NAME);
		String s2 = getFieldDisplayName(EmployeeModel.STATUS_ONLEAVE_NAME);
		String s3 = getFieldDisplayName(EmployeeModel.STATUS_QUIT_NAME);
		String[] statusNames = {s1,s2,s3};
	    GridCellRenderer<EmployeeModel> statusRenderer = MyGridHelper.createListCellRender(EmployeeModel.STATUS, statusCodes, statusNames);
	    column = new ColumnConfig();
		name = EmployeeModel.STATUS;
	    column.setId(name);   
	    column.setHeader(getFieldDisplayName(name));   
	    column.setWidth(200); 
	    column.setRenderer(statusRenderer);
	    configs.add(column);
	    
	    return configs;
	}

	
	public String getFieldDisplayName(String code) {
		return EmployeeFieldsConstant.INSTANCE.getString(code);
	}

	@Override
	public String[] getNotNullFieldNames() {
		return notNullFields;
	}

	@Override
	public EmployeeModel createModelInstance() {
		EmployeeModel model = new EmployeeModel();
		model.setStatus(EmployeeStatus.STATUS_NORMAL);
		return model;
	}
}
